package de.tabuma.wsdl2xform.xform.logik;

import de.tabuma.wsdl2xform.history.logik.JournalEntryFacade;
import de.tabuma.wsdl2xform.xform.entity.Project;
import javax.jdo.*;
import java.util.*;
import java.util.logging.*;
import de.tabuma.soap.entity.wsdl.TDefinitions;
import de.tabuma.wsdl2xform.logik.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.2 $
 * @since $Date: 2009/12/30 01:10:56 $
 */
public class ProjectFacade {

    private static Logger log = Logger.getLogger(ProjectFacade.class.getName());

    public static final int PROJECT_USER = 1;
    public static final int PROJECT_NAME = 2;
    public static final int PROJECT_DESCRIPTION = 3;
    public static final int PROJECT_DROPED = 4;
    public static final int PROJECT_WSDL = 5;
    public static final int PROJECT_URL = 6;
    public static final int PROJECT_METHOD = 7;
    public static final int PROJECT_EXTENDED_MODE = 8;

    /**
     * Creates a project
     * @param user user of the project
     * @param name name of the project
     * @param description description of the project
     * @param wsdlFile file of the wsdlfile
     * @return
     */
    public static int doCreate(String user, String name, String description, String url, String method, boolean extendedMode, Long wsdlFile) {
        int action = JournalEntryFacade.ACTION_CREATE;
        int itemTyp = JournalEntryFacade.PROJECT_ITEM;

        int id = -1;
        try  {
            Project project = new Project(user, name, description, url, method, extendedMode, wsdlFile);
            PersistenceManager pm = PMF.get().getPersistenceManager();
            pm.makePersistent(project);
            id = project.getId().intValue();
            pm.close();

            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_USER, project.getUser());
            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_NAME, project.getName());
            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_DESCRIPTION, project.getDescription());
            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_DROPED, project.getDroped());
            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_URL, project.getUrl());
            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_WSDL, project.getWsdlFile());
            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_METHOD, project.getMethod());
            JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_EXTENDED_MODE, project.getExtendedMode());
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return id;
    }

    /**
     * Edit for
     * @param user
     * @param id
     * @param user1
     * @param name
     * @param description
     */
    public static void doEdit(String user, int id, String user1, String name, String description) {
        Project project = new Project(user1, name, description);
        doUpdate(user,project,JournalEntryFacade.ACTION_UPDATE);
    }

    public static void doRemove(String user, int id) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(Project.class);
        query.setFilter("id == "+id);
        List<Project> list = (List<Project>)query.execute();
        Project project = null;
        if(list.size()!=0) {
            log.fine("The is a project for id == '"+id+"'");
            project = list.get(0);
        }
        pm.close();
        doUpdate(user,project,JournalEntryFacade.ACTION_REMOVE);
    }

    private static void doUpdate(String user,Project newProject, int action) {
        int itemTyp = JournalEntryFacade.PROJECT_ITEM;
        int id = newProject.getId().intValue();

        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(Project.class);
        query.setFilter("id == "+newProject.getId());
        List<Project> list = (List<Project>)query.execute();
        Project oldProject = null;
        if(list.size()!=0) {
            log.fine("The is a project for id == '"+newProject.getId()+"'");
            Project project = list.get(0);
            try {

                oldProject = (Project) project.clone();
            }
            catch(Exception e) {
                log.warning(e.getMessage());
            }
            project.setDescription(newProject.getDescription());
            project.setDroped(newProject.getDroped());
            project.setName(newProject.getName());
            project.setUser(newProject.getUser());
        }
        query.closeAll();
        pm.close();

        if(newProject.getDescription()!=null) {
            if(!newProject.getDescription().equals(oldProject.getDescription())) {
                JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_DESCRIPTION, newProject.getDescription());
            }
        }
        if(newProject.getName()!=null) {
            if(!newProject.getName().equals(oldProject.getName())) {
                JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_NAME, newProject.getName());
            }
        }
        if(newProject.getUser()!=null) {
            if(!newProject.getUser().equals(oldProject.getUser())) {
                JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_USER, newProject.getUser());
            }
        }
        if(newProject.getDroped()!=null) {
            if(!newProject.getDroped().equals(oldProject.getDroped())) {
                JournalEntryFacade.createJournalEntry(itemTyp, id, user, action, PROJECT_USER, newProject.getDroped());
            }
        }
    }

    public static List<Project> getProjectList(String user) {
        ArrayList<Project> antwortList = new ArrayList<Project>();
        try {
            PersistenceManager pm = PMF.get().getPersistenceManager();
            Query query = pm.newQuery(Project.class);
            query.setFilter("user == '"+user+"'");
            log.fine("user == '"+user+"'");
            List<Project> list = (List<Project>)query.execute();
            antwortList.addAll(list);
            query.closeAll();
            pm.close();
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return antwortList;
    }

    public static List<Project> getActivProjectList(String user) {
        ArrayList<Project> antwortList = new ArrayList<Project>();
        try {
            PersistenceManager pm = PMF.get().getPersistenceManager();
            Query query = pm.newQuery(Project.class);
            query.setFilter("user == '"+user+"' && droped == false");
            log.fine("user == '"+user+"' && droped == false");
            Object test = query.execute();
            log.fine(""+test);
            List<Project> list = (List<Project>) test;
            log.fine(""+list);
            antwortList.addAll(list);
            query.closeAll();
            pm.close();
            log.fine(antwortList.toString());
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return antwortList;
    }

    public static List<Project> getDropedProjectList(String user) {
        ArrayList<Project> antwortList = new ArrayList<Project>();
        try {
            PersistenceManager pm = PMF.get().getPersistenceManager();
            Query query = pm.newQuery(Project.class);
            query.setFilter("user == '"+user+"' && droped == true");
            List<Project> list = (List<Project>)query.execute();
            antwortList.addAll(list);
            query.closeAll();
            pm.close();
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return antwortList;
    }

    public static Project getProject(int id) {
        Project project = null;
        try {
            PersistenceManager pm = PMF.get().getPersistenceManager();
            Query query = pm.newQuery(Project.class);
            query.setFilter("id == "+id);
            List<Project> list = (List<Project>)query.execute();
            if(list.size()>0) {
                project = list.get(0);
            }
            query.closeAll();
            pm.close();
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return project;
    }

}
